package com.atguigu.gulimall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@Configuration
public class GulimallCorsConfiguration {
    @Bean
    public CorsWebFilter corsWebFilter(){
        // 是reactive包下的，别导错
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration= new CorsConfiguration();
        // 允许哪些头跨域
        corsConfiguration.addAllowedHeader("*");
        // 允许哪些请求方式
        corsConfiguration.addAllowedMethod("*");
        // 允许哪个请求来源
        corsConfiguration.addAllowedOrigin("*");
        // 是否允许携带cookie
        corsConfiguration.setAllowCredentials(true);

        // 任意路径我们都要跨域配置
        source.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(source);
    }
}
